package com.hunttown.mes.manage.service;

import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.AnalysisManageRightMenuRpcService;
import com.hunttown.mes.rpc.api.AnalysisManageRightRoleMenuRelRpcService;
import com.hunttown.mes.rpc.domain.AnalysisManageRightRoleMenuRelDTO;
import com.hunttown.mes.rpc.domain.query.AnalysisManageRightRoleMenuRelDTOQuery;
import com.hunttown.mes.rpc.domain.AnalysisManageRightMenuDTO;
import com.hunttown.mes.rpc.domain.query.AnalysisManageRightMenuDTOQuery;
import com.hunttown.common.domain.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.hunttown.mes.common.utils.FormatUtils.formatCommas;

/**
 * created by hunttown on 2018-10-26 15:04:59
 */
@Service
public class AnalysisManageRightRoleMenuRelManageService {

    private final static Logger logger = LoggerFactory.getLogger(AnalysisManageRightRoleMenuRelManageService.class);

    private final AnalysisManageRightRoleMenuRelRpcService rpcService;
    private final AnalysisManageRightMenuRpcService rightMenuRpcService;

    @Autowired
    public AnalysisManageRightRoleMenuRelManageService(AnalysisManageRightRoleMenuRelRpcService rpcService, AnalysisManageRightMenuRpcService rightMenuRpcService) {
        this.rpcService = rpcService;
        this.rightMenuRpcService = rightMenuRpcService;
    }

    // 新增
    public AnalysisManageRightRoleMenuRelDTO insert(AnalysisManageRightRoleMenuRelDTO objDTO) {
        return rpcService.insert(objDTO);
    }

    // 通过ID修改
    public Boolean updateInfoById(AnalysisManageRightRoleMenuRelDTO objDTO) {
        return rpcService.updateInfoById(objDTO);
    }

    // 通过Query修改
    public Boolean updateInfoByQuery(AnalysisManageRightRoleMenuRelDTOQuery objDTOQuery) {
        return rpcService.updateInfoByQuery(objDTOQuery);
    }

    // 通过ID获取
    public AnalysisManageRightRoleMenuRelDTO getById(Integer id) {
        if (id <= 0) {
            return null;
        }
        return rpcService.selectById(id);
    }

    // 通过Query获取
    public AnalysisManageRightRoleMenuRelDTO getByQuery(AnalysisManageRightRoleMenuRelDTOQuery objDTOQuery) {
        return rpcService.selectObjByQuery(objDTOQuery);
    }

    // 获取分页列表
    public Page<AnalysisManageRightRoleMenuRelDTO> getForPage(AnalysisManageRightRoleMenuRelDTOQuery objDTOQuery) {
        return rpcService.getListForPage(objDTOQuery);
    }

    //批量插入
    public void insrtForBatch(Integer roleId, String menuIds) {
        AnalysisManageRightRoleMenuRelDTO obj = null;
        List<AnalysisManageRightRoleMenuRelDTO> list = new ArrayList<>();

        if (roleId <= 0) {
            return;
        }

        //处理字符串中的","
        menuIds = formatCommas(menuIds);

        if (StringUtils.isBlank(menuIds)) {
            return;
        }

        String[] ids = menuIds.split(",");

        for (String item : ids) {
            if (item.equals("on")) {
                continue;
            }

            obj = new AnalysisManageRightRoleMenuRelDTO();
            obj.setRoleId(roleId);
            obj.setMenuId(Integer.valueOf(item));
            obj.setDeleteFlag(0);

            list.add(obj);
        }

        rpcService.insertForBatch(list);
    }

    public List<Integer> getLastParentIdByMenuId(Integer id) {
        List<Integer> pId = new ArrayList<>();

        AnalysisManageRightMenuDTOQuery query = new AnalysisManageRightMenuDTOQuery();
        query.setPageSize(10000);
        Page<AnalysisManageRightMenuDTO> menuPage = rightMenuRpcService.getListForPage(query);

        if (menuPage != null) {
            List<AnalysisManageRightMenuDTO> listPage = menuPage.getItems();
            pId = getLastParentId(listPage, id);
        }

        return pId;
    }

    //使用一个递归查询
    public List<Integer> getLastParentId(List<AnalysisManageRightMenuDTO> list, Integer id) {
        List<Integer> pIdList = new ArrayList<>();

        if (list == null || list.size() == 0) {
            return pIdList;
        }

        List<AnalysisManageRightMenuDTO> filterList = list.stream().filter(s -> s.getId().equals(id)).collect(Collectors.toList());

        if (filterList == null || filterList.size() == 0) {
            return pIdList;
        }
        pIdList.add(filterList.get(0).getId());

        List<Integer> pList = getLastParentId(list, filterList.get(0).getParentId());
        for (Integer pid : pList) {
            pIdList.add(pid);
        }

        return pIdList;
    }

    //物理删除
    //请注意：此方法是通过role_id 进行删除操作的。
    public Boolean deletePysical(Integer roleId) {
        if (roleId == null || roleId <= 0) {
            return false;
        }
        return rpcService.deletePhysicalByRoleId(roleId);
    }
}